import { createRouter, createWebHashHistory } from 'vue-router'

const routes = [
  {
    path: '/login',
    name: 'login',
    component: () => import('~/views/LogIn.vue')
  },
  {
    path: '/',
    name: 'home',
    component: () => import('~/views/shared/Workbench.vue'),
    children: [
      {
        path: '/',
        name: 'home_default',
        component: () => import('~/views/Home.vue')
      }
    ]
  },
  {
    path: '/dbs',
    name: 'databases',
    component: () => import('~/views/shared/Workbench.vue'),
    children: [
      {
        path: '',
        name: 'databases_index',
        component: () => import('~/views/db/Index.vue')
      }
    ]
  },
  {
    path: '/db',
    component: () => import('~/views/shared/Content.vue'),
    children: [
      {
        name: 'db_details',
        path: ':dbIndex/:schema/details',
        component: () => import('~/views/db/Details.vue'),
      }
    ]
  },
  {
    path: '/shares',
    name: 'shares',
    component: () => import('~/views/shared/Workbench.vue'),
    children: [
      {
        path: 'list',
        name: 'shares_list',
        component: () => import('~/views/shares/List.vue')
      }
    ]
  },
  {
    path: '/share',
    name: 'share',
    component: () => import('~/views/shared/Content.vue'),
    children: [
      {
        path: ':code/details',
        name: 'share_details',
        component: () => import('~/views/shares/Details.vue')
      },
      {
        path: 'profit',
        name: 'share_profit',
        component: () => import('~/views/shares/Profit.vue')
      }
    ]
  },
  {
    path: '/tools',
    name: 'tools',
    component: () => import('~/views/shared/Workbench.vue'),
    children: [
      {
        path: '',
        name: 'tools_index',
        component: () => import('~/views/tools/Index.vue')
      }
    ]
  },
  {
    path: '/settings',
    name: 'settings',
    component: () => import('~/views/shared/Workbench.vue'),
    children: [
      {
        path: '',
        name: 'settings_index',
        component: () => import('~/views/settings/Index.vue')
      }
    ]
  },
  {
    path: '/setting',
    name: 'setting',
    component: () => import('~/views/shared/Content.vue'),
    children: [
      {
        path: 'about',
        component: () => import('~/views/settings/About.vue')
      }
    ]
  },
  {
    path: '/:catchAll(.*)*',
    name: 'notfound',
    component: () => import('~/views/shared/Workbench.vue'),
    children: [{
      path: '',
      name: 'notfound_404',
      component: () => import('~/views/shared/404.vue')
    }]
  }
];

const router = createRouter({
  history: createWebHashHistory('/'),
  routes
});

let vueApp = null;

let proxy = new Proxy(router.install, {
  apply: function (target, ctx, args) {
    vueApp = args[0];
    vueApp.provide('$router', router);

    Reflect.apply(...arguments);
  }
});

router.install = proxy;

router.beforeEach(function (to, from, next) {
  let store = vueApp._context.provides.store;

  if (sessionStorage.getItem('auth')) {
    try {
      let auth = JSON.parse(sessionStorage.getItem('auth'));

      store.commit('fillConfigs', auth);
    } catch {

    }
  }

  if (to.path == '/login' || to.path == '/failed') {
    next();

    return;
  }

  let query = to.query;

  if (!store.state.user.authenticated) {
    router.push({ path: '/login', query: query });

    return;
  } else {
    next();
  }
});

export default router;